package cn.iocoder.yudao.module.shop.controller.admin.price;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.shop.dal.dataobject.item.ShopItemDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.priceitem.ShopPriceItemDO;
import cn.iocoder.yudao.module.shop.dal.mysql.item.ShopItemMapper;
import cn.iocoder.yudao.module.shop.service.priceitem.ShopPriceItemService;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.price.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.price.ShopPriceDO;
import cn.iocoder.yudao.module.shop.convert.price.ShopPriceConvert;
import cn.iocoder.yudao.module.shop.service.price.ShopPriceService;

@Tag(name = "管理后台 - 店内价格套餐")
@RestController
@RequestMapping("/shop/price")
@Validated
public class ShopPriceController {

    @Autowired
    private ShopPriceService shopPriceService;

    @Autowired
    private ShopPriceItemService shopPriceItemService;

    @Resource
    private ShopItemMapper shopItemMapper;

    @PostMapping("/create")
    @Operation(summary = "创建店内价格套餐")
    @PreAuthorize("@ss.hasPermission('shop:price:create')")
    public CommonResult<Long> createPrice(@Valid @RequestBody ShopPriceCreateReqVO createReqVO) {
        return success(shopPriceService.createPrice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店内价格套餐")
    @PreAuthorize("@ss.hasPermission('shop:price:update')")
    public CommonResult<Boolean> updatePrice(@Valid @RequestBody ShopPriceUpdateReqVO updateReqVO) {
        shopPriceService.updatePrice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店内价格套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:price:delete')")
    public CommonResult<Boolean> deletePrice(@RequestParam("id") Long id) {
        shopPriceService.deletePrice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店内价格套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:price:query')")
    public CommonResult<ShopPriceRespVO> getPrice(@RequestParam("id") Long id) {
        ShopPriceDO price = shopPriceService.getPrice(id);
        return success(ShopPriceConvert.INSTANCE.convert(price));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店内价格套餐列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:price:query')")
    public CommonResult<List<ShopPriceRespVO>> getPriceList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopPriceDO> list = shopPriceService.getPriceList(ids);
        return success(ShopPriceConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/updatedefault")
    public CommonResult<Boolean> changeDefault(@Valid @RequestBody ShopPriceDefaultReqVO reqVO){
        shopPriceService.changeDefault(reqVO.getPriceId(), reqVO.getTenantId());

        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得店内价格套餐分页")
    @PreAuthorize("@ss.hasPermission('shop:price:query')")
    public CommonResult<PageResult<ShopPriceRespVO>> getPricePage(@Valid ShopPricePageReqVO pageVO) {
        PageResult<ShopPriceDO> pageResult = shopPriceService.getPricePage(pageVO);

        List<Long> priceIdList = new ArrayList<>();
        pageResult.getList().forEach(shopPriceDO -> {
            priceIdList.add(shopPriceDO.getId());
        });

        List<ShopItemDO> shopItemDOList = shopItemMapper.selectList(new LambdaQueryWrapper<ShopItemDO>()
                .eq(ShopItemDO::getTenantId, pageVO.getTenantId()));

        List<Long> itemIdList = CollectionUtils.convertList(shopItemDOList, ShopItemDO::getId);

        List<ShopPriceItemDO> shopPriceItemDOList = shopPriceItemService.getPriceItemListByPriceIdAndItemId(priceIdList, itemIdList);

        Map<Long, List<ShopPriceItemDO>> shopPriceItemDOMap = CollectionUtils.convertMultiMap(shopPriceItemDOList, ShopPriceItemDO::getPriceId);

        return success(ShopPriceConvert.INSTANCE.convertPage(pageResult, shopPriceItemDOMap));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店内价格套餐 Excel")
    @PreAuthorize("@ss.hasPermission('shop:price:export')")
    @OperateLog(type = EXPORT)
    public void exportPriceExcel(@Valid ShopPriceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopPriceDO> list = shopPriceService.getPriceList(exportReqVO);
        // 导出 Excel
        List<ShopPriceExcelVO> datas = ShopPriceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店内价格套餐.xls", "数据", ShopPriceExcelVO.class, datas);
    }

}
